﻿using System;
using System;
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using netDxf;
using netDxf.Blocks;
using netDxf.Collections;
using netDxf.Entities;
using netDxf.Header;
using netDxf.Objects;
using netDxf.Tables;
using netDxf.Units;
using Attribute = netDxf.Entities.Attribute;
using Image = netDxf.Entities.Image;
using Point = netDxf.Entities.Point;
using Trace = netDxf.Entities.Trace;
using System.Threading;

namespace TestDxfDocument
{

    class _ReplaceableInsertWithChipFlute2
    {
        public static void ReplaceableInsertWithChipFlute2(Dictionary<string, Object> data)//自锁式可换钻头刀头
        {
            DxfDocument dxf = new DxfDocument();
            //图层
            Layer contour = new Layer("contour");//轮廓层
            Layer dimensioning = new Layer("dimensioning");//尺寸层
            Layer refin = new Layer("refin");//基准线/内轮廓/虚线
            Layer center = new Layer("center");//中心线
            Layer textlayer = new Layer("textlayer");//文本层
            Layer hatching = new Layer("hatching");//填充层
            Layer additionalline = new Layer("additionalline");//附加线
            Layer cutting = new Layer("cutting");//切削层
            fun.SetLayer(dxf);



            //参数

            double offset = 10;
            double textsize = 3.5;

            double pi = Math.PI;
            string path = Convert.ToString(data["path"]);

            //double scale = 1; //放大系数
            double scale = Convert.ToDouble(data["scale"]);


            double t = Convert.ToDouble(data["lpr"])*scale;
            double d = Convert.ToDouble(data["dconms"]) * scale;
            double dc = Convert.ToDouble(data["dc"]) * scale;
            double ls = Convert.ToDouble(data["ls"]) * scale;
            double oal = ls + t;

            //主视图
            Vector2 v1 = new Vector2(0, 0);
            Vector2 v2 = new Vector2(v1.X + oal + 4, v1.Y);
            Vector2 v3 = new Vector2(v1.X + 2 + t / 9.516, v1.Y);
            Vector2 v4 = new Vector2(v3.X + dc / 2 / Math.Tan(75 * pi / 180), v3.Y + dc / 2);
            Vector2 v5 = new Vector2(v3.X + t - t / 9.516, v4.Y);
            Vector2 v6 = new Vector2(v5.X, v3.Y + d / 2);
            Vector2 v7 = new Vector2(v6.X + ls - 0.7, v6.Y);
            Vector2 v8 = new Vector2(v7.X + 0.7, v7.Y - 0.7);
            Vector2 v9 = new Vector2(v8.X, v8.Y * -1);
            Vector2 v10 = new Vector2(v7.X, v7.Y * -1);
            Vector2 v11 = new Vector2(v6.X, v6.Y * -1);
            Vector2 v12 = new Vector2(v5.X, v5.Y * -1);
            Vector2 v13 = new Vector2(v4.X, v4.Y * -1);
            Vector2 v14 = new Vector2(v3.X + dc / 7.973 / Math.Tan(75 * pi / 180), v3.Y - dc / 7.973);
            Vector2 v15 = new Vector2(v1.X + 2, v1.Y);
            Vector2 v16 = new Vector2(v14.X, v14.Y * -1);
            Vector2 v17 = new Vector2(v6.X, v6.Y - d / 1.655);
            Vector2 v18 = new Vector2(v8.X, v17.Y - ls * Math.Tan(6 * pi / 180));
            Vector2 v19 = new Vector2(v7.X, v17.Y - (ls - 0.7) * Math.Tan(6 * pi / 180));

            //左视图
            Vector2 v20 = new Vector2(v2.X + oal, v2.Y);
            Vector2 v21 = new Vector2(v20.X + dc + 4, v20.Y);
            Vector2 v22 = new Vector2(v20.X + dc / 2 + 2, v20.Y);
            Vector2 v23 = new Vector2(v22.X, v22.Y + dc / 2 + 2);
            Vector2 v24 = new Vector2(v22.X, v22.Y - dc / 2 - 2);
            Vector2 v25 = fun.Polar(v22, 120 * pi / 180, dc / 2.093);
            Vector2 v26 = fun.Polar(v22, 51 * pi / 180, dc / 2.093);
            Vector2 v27 = new Vector2(v26.X - dc / 11.727, v26.Y - dc / 9.882);
            Vector2 v28 = new Vector2(v26.X - dc / 7.788, v26.Y - dc / 5.1);
            Vector2 v29 = fun.Polar(v28, -49 * pi / 180, dc / 11.04);
            Vector2 v30 = new Vector2(v29.X, v29.Y - dc / 6.639);
            Vector2 v31 = fun.Polar(v30, -169 * pi / 180, dc / 24.909);
            Vector2 v32 = fun.Polar(v31, -107 * pi / 180, dc / 31.072);
            Vector2 v33 = fun.Polar(v32, -156 * pi / 180, dc / 34.25);
            Vector2 v34 = fun.Polar(v33, -95 * pi / 180, dc / 17.834);
            Vector2 v35 = fun.rotatePoint_Changed(v22, v25, 180);
            Vector2 v36 = fun.Polar(v34, 127 * pi / 180, dc / 5.796);
            Vector2 v37 = new Vector2(v29.X, v29.Y - dc / 8.257);
            Vector2 v38 = fun.Polar(v37, -149 * pi / 180, dc / 17.028);
            Vector2 v39 = fun.Polar(v38, 135 * pi / 180, dc / 6.385);
            Vector2 v40 = fun.Polar(v38, 135 * pi / 180, dc / 7.405);
            Vector2 v41 = fun.Polar(v28, -49 * pi / 180, dc / 32.761);
            Vector2 v42 = new Vector2(v40.X + dc / 52.877, v40.Y + dc / 52.877 * (v41.Y - v40.Y) / (v41.X - v40.X));
            Vector2 v43 = fun.Polar(v34, 127 * pi / 180, dc / 12.558);
            Vector2 v44 = fun.Polar(v22, 119 * pi / 180, dc / 2);
            Vector2 v45 = fun.Polar(v22, 95 * pi / 180, dc / 2);
            Vector2 v46 = fun.Polar(v22, 94 * pi / 180, dc / 2.093);
            Vector2 v47 = new Vector2(v46.X, v46.Y - dc / 8.6);
            Vector2 v49 = fun.Polar(v22, 86 * pi / 180, dc / 2.093);
            Vector2 v48 = new Vector2(v49.X, v49.Y - dc / 8.6);

            Vector2 a26 = fun.rotatePoint_Changed(v22, v26, 180);
            Vector2 a27 = fun.rotatePoint_Changed(v22, v27, 180);
            Vector2 a28 = fun.rotatePoint_Changed(v22, v28, 180);
            Vector2 a29 = fun.rotatePoint_Changed(v22, v29, 180);
            Vector2 a30 = fun.rotatePoint_Changed(v22, v30, 180);
            Vector2 a31 = fun.rotatePoint_Changed(v22, v31, 180);
            Vector2 a32 = fun.rotatePoint_Changed(v22, v32, 180);
            Vector2 a33 = fun.rotatePoint_Changed(v22, v33, 180);
            Vector2 a34 = fun.rotatePoint_Changed(v22, v34, 180);
            Vector2 a35 = fun.rotatePoint_Changed(v22, v35, 180);
            Vector2 a36 = fun.rotatePoint_Changed(v22, v36, 180);
            Vector2 a37 = fun.rotatePoint_Changed(v22, v37, 180);
            Vector2 a38 = fun.rotatePoint_Changed(v22, v38, 180);
            Vector2 a39 = fun.rotatePoint_Changed(v22, v39, 180);
            Vector2 a40 = fun.rotatePoint_Changed(v22, v40, 180);
            Vector2 a41 = fun.rotatePoint_Changed(v22, v41, 180);
            Vector2 a42 = fun.rotatePoint_Changed(v22, v42, 180);
            Vector2 a43 = fun.rotatePoint_Changed(v22, v43, 180);
            Vector2 a44 = fun.rotatePoint_Changed(v22, v44, 180);
            Vector2 a45 = fun.rotatePoint_Changed(v22, v45, 180);
            Vector2 a46 = fun.rotatePoint_Changed(v22, v46, 180);
            Vector2 a47 = fun.rotatePoint_Changed(v22, v47, 180);
            Vector2 a48 = fun.rotatePoint_Changed(v22, v48, 180);
            Vector2 a49 = fun.rotatePoint_Changed(v22, v49, 180);

            Vector2 v50 = new Vector2(v22.X, v22.Y + dc / 2);
            Vector2 v51 = new Vector2(v22.X, v22.Y - dc / 2);







            Line line1 = new Line(v1, v2);
            line1.Layer = center;
            line1.Linetype = Linetype.DashDot;
            line1.LinetypeScale = 3;
            dxf.AddEntity(line1);

            LwPolyline line2 = new LwPolyline();
            line2.Vertexes.Add(new LwPolylineVertex(v3));
            line2.Vertexes.Add(new LwPolylineVertex(v4));
            line2.Vertexes.Add(new LwPolylineVertex(v5));
            line2.Vertexes.Add(new LwPolylineVertex(v6));
            line2.Vertexes.Add(new LwPolylineVertex(v7));
            line2.Vertexes.Add(new LwPolylineVertex(v8));
            line2.Vertexes.Add(new LwPolylineVertex(v9));
            line2.Vertexes.Add(new LwPolylineVertex(v10));
            line2.Vertexes.Add(new LwPolylineVertex(v11));
            line2.Vertexes.Add(new LwPolylineVertex(v12));
            line2.Vertexes.Add(new LwPolylineVertex(v13));
            line2.Vertexes.Add(new LwPolylineVertex(v3));
            fun.PolyFill(dxf, line2, 0.1, contour);

            LwPolyline line3 = new LwPolyline();
            line3.Vertexes.Add(new LwPolylineVertex(v16));
            line3.Vertexes.Add(new LwPolylineVertex(v15));
            line3.Vertexes.Add(new LwPolylineVertex(v14));
            fun.PolyFill(dxf, line3, 0.1, contour);

            LwPolyline line4 = new LwPolyline();
            line4.Vertexes.Add(new LwPolylineVertex(v6));
            line4.Vertexes.Add(new LwPolylineVertex(v17));
            line4.Vertexes.Add(new LwPolylineVertex(v18));
            line4.Vertexes.Add(new LwPolylineVertex(v19));
            line4.Vertexes.Add(new LwPolylineVertex(v7));
            fun.PolyFill(dxf, line4, 0.1, contour);

            Line line5 = new Line(v20, v21);
            line5.Layer = center;
            line5.Linetype = Linetype.DashDot;
            line5.LinetypeScale = 3;
            dxf.AddEntity(line5);

            Line line6 = new Line(v23, v24);
            line6.Layer = center;
            line6.Linetype = Linetype.DashDot;
            line6.LinetypeScale = 3;
            dxf.AddEntity(line6);

            Arc arc1 = fun.DrawArc(a26, v22, v35);
            fun.drawBArc(dxf, arc1, 0.1, contour);

            Arc arc2 = fun.DrawArc3(a37, a38, a33);
            fun.drawBArc(dxf, arc2, 0.1, contour);

            LwPolyline line7 = new LwPolyline();
            line7.Vertexes.Add(new LwPolylineVertex(a26));
            line7.Vertexes.Add(new LwPolylineVertex(a27));
            line7.Vertexes.Add(new LwPolylineVertex(a28));
            line7.Vertexes.Add(new LwPolylineVertex(a29));
            line7.Vertexes.Add(new LwPolylineVertex(a30));
            line7.Vertexes.Add(new LwPolylineVertex(a31));
            line7.Vertexes.Add(new LwPolylineVertex(a32));
            line7.Vertexes.Add(new LwPolylineVertex(a33));
            line7.Vertexes.Add(new LwPolylineVertex(a34));
            line7.Vertexes[1].Bulge = Math.Tan(82 * pi / 180 / 4);
            line7.Vertexes[7].Bulge = Math.Tan(28 * pi / 180 / 4);
            fun.PolyFill(dxf, line7, 0.1, contour);

            LwPolyline line8 = new LwPolyline();
            line8.Vertexes.Add(new LwPolylineVertex(a35));
            line8.Vertexes.Add(new LwPolylineVertex(a34));
            line8.Vertexes.Add(new LwPolylineVertex(a36));
            line8.Vertexes.Add(new LwPolylineVertex(a39));
            line8.Vertexes.Add(new LwPolylineVertex(a38));
            line8.Vertexes.Add(new LwPolylineVertex(a40));
            line8.Vertexes.Add(new LwPolylineVertex(a41));
            line8.Vertexes[0].Bulge = Math.Tan(-24 * pi / 180 / 4);
            line8.Vertexes[2].Bulge = Math.Tan(-75 * pi / 180 / 4);
            fun.PolyFill(dxf, line8, 0.1, contour);

            Arc arc3 = fun.DrawArc(v26, v22, v25);
            fun.drawBArc(dxf, arc3, 0.1, contour);

            Arc arc4 = fun.DrawArc3(v37, v38, v33);
            fun.drawBArc(dxf, arc4, 0.1, contour);

            LwPolyline line9 = new LwPolyline();
            line9.Vertexes.Add(new LwPolylineVertex(v26));
            line9.Vertexes.Add(new LwPolylineVertex(v27));
            line9.Vertexes.Add(new LwPolylineVertex(v28));
            line9.Vertexes.Add(new LwPolylineVertex(v29));
            line9.Vertexes.Add(new LwPolylineVertex(v30));
            line9.Vertexes.Add(new LwPolylineVertex(v31));
            line9.Vertexes.Add(new LwPolylineVertex(v32));
            line9.Vertexes.Add(new LwPolylineVertex(v33));
            line9.Vertexes.Add(new LwPolylineVertex(v34));
            line9.Vertexes[1].Bulge = Math.Tan(82 * pi / 180 / 4);
            line9.Vertexes[7].Bulge = Math.Tan(28 * pi / 180 / 4);
            fun.PolyFill(dxf, line9, 0.1, contour);

            LwPolyline line10 = new LwPolyline();
            line10.Vertexes.Add(new LwPolylineVertex(v35));
            line10.Vertexes.Add(new LwPolylineVertex(v34));
            line10.Vertexes.Add(new LwPolylineVertex(v36));
            line10.Vertexes.Add(new LwPolylineVertex(v39));
            line10.Vertexes.Add(new LwPolylineVertex(v38));
            line10.Vertexes.Add(new LwPolylineVertex(v40));
            line10.Vertexes.Add(new LwPolylineVertex(v41));
            line10.Vertexes[0].Bulge = Math.Tan(-24 * pi / 180 / 4);
            line10.Vertexes[2].Bulge = Math.Tan(-75 * pi / 180 / 4);
            fun.PolyFill(dxf, line10, 0.1, contour);

            Arc arc5 = fun.DrawArc(v42, v22, a43);
            fun.drawBArc(dxf, arc5, 0.1, contour);

            Arc arc6 = fun.DrawArc(a42, v22, v43);
            fun.drawBArc(dxf, arc6, 0.1, contour);

            LwPolyline line11 = new LwPolyline();
            line11.Vertexes.Add(new LwPolylineVertex(v25));
            line11.Vertexes.Add(new LwPolylineVertex(v44));
            fun.PolyFill(dxf, line11, 0.1, contour);

            Arc arc7 = fun.DrawArc(v45, v22, v44);
            fun.drawBArc(dxf, arc7, 0.1, contour);

            LwPolyline line12 = new LwPolyline();
            line12.Vertexes.Add(new LwPolylineVertex(v45));
            line12.Vertexes.Add(new LwPolylineVertex(v46));
            line12.Vertexes.Add(new LwPolylineVertex(v47));
            line12.Vertexes.Add(new LwPolylineVertex(v48));
            line12.Vertexes.Add(new LwPolylineVertex(v49));
            fun.PolyFill(dxf, line12, 0.1, contour);

            LwPolyline line13 = new LwPolyline();
            line13.Vertexes.Add(new LwPolylineVertex(v35));
            line13.Vertexes.Add(new LwPolylineVertex(a44));
            fun.PolyFill(dxf, line13, 0.1, contour);

            Arc arc8 = fun.DrawArc(a45, v22, a44);
            fun.drawBArc(dxf, arc8, 0.1, contour);

            LwPolyline line14 = new LwPolyline();
            line14.Vertexes.Add(new LwPolylineVertex(a45));
            line14.Vertexes.Add(new LwPolylineVertex(a46));
            line14.Vertexes.Add(new LwPolylineVertex(a47));
            line14.Vertexes.Add(new LwPolylineVertex(a48));
            line14.Vertexes.Add(new LwPolylineVertex(a49));
            fun.PolyFill(dxf, line14, 0.1, contour);


            //标注

            double height = textsize;
            double arrowsize = textsize;
            double exline = 2;


            fun.dimrot1(dxf, v15, v12, 180, offset + Math .Abs (v15.Y-v12.Y)/2 , 1, height, arrowsize, exline, "LPR=" + t, "", "");

            fun.dimrot1(dxf, v7, v10, -90, offset , 1, height, arrowsize, exline, "d=" + d, "", "");

            fun.dimrot1(dxf, v50, v51, -90, offset+a44.X-a49.X, 1, height, arrowsize, exline, "DC=" + dc, "", "");

            fun.jdbz1(dxf, new Line(v3, fun.Polar(v3, 115 * pi / 180, 0.1)), new Line(v3, fun.Polar(v3, -115 * pi / 180, 0.1)), offset, "150°", 5);


            double isframe = Convert.ToInt32(data["isframe"]);
            String isframe_chose = Convert.ToString(data["isframe_chose"]);
            Vector2 p0 = new Vector2(0, 0);
            if (isframe == 1)
            {
                if (isframe_chose.Equals("0"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA0X(dxf, new Vector2(p0.X - 1189 / 2.0, p0.Y + 841 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("1"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA1X(dxf, new Vector2(p0.X - 841 / 2.0, p0.Y + 594 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("2"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA2X(dxf, new Vector2(p0.X - 594 / 2.0, p0.Y + 420 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("3"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA3X(dxf, new Vector2(p0.X - 420 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4X"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4X(dxf, new Vector2(p0.X - 297 / 2.0, p0.Y + 210 / 2.0 - 10 * 2.26), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4Y"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4Y(dxf, new Vector2(p0.X - 210 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }
            }
            fun.SetLayer(dxf);
            fun.SetByLayer(dxf);


            //定义格式，路径导出
            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);


        }

}
}